package cn.ejiajunxy.manager;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.leo.baseui.ui.BaseActivity;

import cn.ejiajunxy.manager.fragment.FragmentMine;
import cn.ejiajunxy.manager.fragment.FragmentOrder;

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {
    private int mLastTabIndex;
    private TabLayout mTabLayout;
    private FragmentManager mFm;
    private final int mFragmentContainer = R.id.fragmentContainer;
    private String mTagsTitles[] = {"订单", "我"};
    private Class<?> mFragments[] = {FragmentOrder.class, FragmentMine.class};

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void init() {
        mFm = getSupportFragmentManager();
        initViews();
    }

    private void initViews() {
        mTabLayout = findViewById(R.id.tab);
        mTabLayout.addOnTabSelectedListener(this);
        for (int i = 0; i < mTagsTitles.length; i++) {
            View tabView = LayoutInflater.from(this).inflate(R.layout.item_tab, mTabLayout, false);
            //mTabLayout.newTab().setIcon() 会由显示问题
            mTabLayout.addTab(mTabLayout.newTab().setCustomView(tabView)
                    .setText(mTagsTitles[i]));
        }
    }

    /**
     * 捕捉返回事件按钮
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                exitApp();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    private long exitTime = 0;

    private void exitApp() {
        // 判断2次点击事件时间
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            exitTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        final int index = tab.getPosition();
        replaceFragment(index);
        mLastTabIndex = index;
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private void replaceFragment(int index) {
        FragmentTransaction ft = mFm.beginTransaction();
        Fragment lf = mFm.findFragmentByTag(mTagsTitles[mLastTabIndex]);
        if (lf != null) {
            ft.detach(lf);
        }
        Fragment f = mFm.findFragmentByTag(mTagsTitles[index]);
        if (f == null) {
            f = Fragment.instantiate(this, mFragments[index].getName());
            ft.add(mFragmentContainer, f, mTagsTitles[index]);
        } else {
            ft.attach(f);
        }
        ft.commit();
    }
}
